package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.makerspace.domain.BookChapterDO;
import com.example.makerspace.dto.BookChapterDTO;
import com.example.makerspace.mapper.BookChapterMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class BookChapterDAO extends BaseDAO<BookChapterDO> {

    public final static String BOOK_ID = "book_id";
    public final static String CHAPTER_NUM = "chapter_num";

    @Resource
    private BookChapterMapper bookChapterMapper;

    @PostConstruct
    public void init(){
        this.mapper = bookChapterMapper;
    }

    public BookChapterDO selectDuplicate(BookChapterDTO bookChapterDTO){
        return bookChapterMapper.selectOne(new QueryWrapper<BookChapterDO>()
                .eq(BOOK_ID, bookChapterDTO.getBookId())
                .eq(CHAPTER_NUM, bookChapterDTO.getChapterNum()));
    }

    public List<BookChapterDO> selectByBookId(Integer bookId) {
        return bookChapterMapper.selectList(new QueryWrapper<BookChapterDO>()
                .eq(BOOK_ID, bookId).orderByAsc(CHAPTER_NUM));
    }
}
